package com.chengzhi.structure;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @description:
 * @author: ruanchengzhi
 * @create: 2020-08-20 09:53
 **/
public class MaxHeapTest {
    public static void main(String[] args) throws InterruptedException {
        PriorityQueue<Mission> pq = new PriorityQueue<>(Comparator.comparing(v -> v.time));
        LocalDateTime time = LocalDateTime.now();
        pq.add(new Mission(time.plusSeconds(10), "2"));
        pq.add(new Mission(time.plusSeconds(15), "3"));
        pq.add(new Mission(time.plusSeconds(5), "1"));
        pq.add(new Mission(time.plusSeconds(30), "6"));
        pq.add(new Mission(time.plusSeconds(20), "4"));
        pq.add(new Mission(time.plusSeconds(25), "5"));
        while (!pq.isEmpty()) {
            LocalDateTime compareTime = LocalDateTime.now();
            if (pq.peek().time.isBefore(compareTime)) {
                Mission poll = pq.poll();
                System.out.println(poll.data + "   " + poll.time);
            }
            Duration duration = Duration.between(compareTime, pq.peek().time);
            System.out.println(duration.toMillis());
            Thread.sleep(duration.toMillis());
        }
    }

    public static class Mission {
        private LocalDateTime time;
        private String data;

        public Mission(LocalDateTime time, String data) {
            this.time = time;
            this.data = data;
        }
    }
}
